/* Copyright 2009 CtrlSpace Platform
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributor(s):
 * 	Marc Obaldo (marcklaser@gmail.com)
 *  Richelle Co (co.richelle@gmail.com)
 */

dojo.provide("ctrlspace.ui.GetDeploymentDetailsDialog");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dojox.validate.web");

dojo.declare("ctrlspace.ui.GetDeploymentDetailsDialog", [dijit._Widget, dijit._Templated], {
	templateString : dojo.cache("ctrlspace.ui", "templates/GetDeploymentDetailsDialog.html"),
	widgetsInTemplate: true,
		
	// Post create
	postCreate : function() {
		this.inherited(arguments);
	},
	
	// Methods
	show: function() {
		this.dialog.show();
	},
	
	hide: function()
	{
		this.dialog.destroyRecursive();
	},
	
	destroy: function(preserveDom) {
		this.inherited(arguments);
	},
	
	getDeploymentDetails: function(email, password, applicationId, proxy) {
		var projectId;
		if (IDE.getUI().tree.lastFocused.item.projectId != null) {
			projectId = IDE.getUI().tree.lastFocused.item.projectId[0];
		} else {
			projectId = IDE.getUI().tree.lastFocused.item.id[0];
		}
		var call = dojo.xhrPost({
			url: ctrlspace.config.projectManagerUrl,
			content: {
				command: "get_deployment_details",
				email: email,
				password: password,
				applicationId: applicationId,
				proxy: proxy,
				projectId: projectId 
			},
			handleAs: 'json',
			load: dojo.hitch(this, this.getDeploymentDetailsSuccess),
			error: dojo.hitch(this, this.getDeploymentDetailsFailure)
		});
	},
	
	getDeploymentDetailsSuccess: function(details) {
		// success, destroy dialog and update UI
		this.hide();
		this.destroy();
		IDE.UI.setStatus('Success.', ctrlspace.config.STATUS_SUCCESS, 5);
	},
	
	getDeploymentDetailsFailure: function(error) {
	},
	
	// Template events
	_submit: function() {
		if (!this.email.isValid()) {
			this.email.displayMessage(this.email.getErrorMessage());
			return;
		}
		
		dojo.attr(this.submitButton, 'disabled', true);
		
		var email = this.email.value;
		var password = this.password.value;
		var applicationId = this.applicationId.value;
		var proxy = this.proxy.value;
		
		IDE.UI.setStatus('Getting details...', ctrlspace.config.STATUS_PROGRESS);

		this.getDeploymentDetails(email, password, applicationId, proxy);
	}
});